package org.king.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import org.king.entity.vo.ActivityEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

public class CommentController implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(CommentController.class);
    //被新增或修改的对象
    private ActivityEntity activity = null;
    //引用主控制器，以便回调它的方法
    private MainController mainController;

    @FXML
    private Text name;
    @FXML
    private Text describe;
    @FXML
    private Spinner<Integer> score;
    @FXML
    private Button btnOk;
    @FXML
    private Button btnCancel;
    @FXML
    private TextField comment;

    public void setActivity(ActivityEntity act) {
        this.activity = act;
        initDataBind();
    }

    public MainController getMainController() {
        return mainController;
    }

    public void setMainController(MainController mainController) {
        this.mainController = mainController;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initializeSpinners();

        btnOk.setOnAction(event -> {
           mainController.onUpdateJoinActivityScore(activity,score.getValue(),comment.getText());
        });
        btnCancel.setOnAction(event -> {
            mainController.onCancelActivity();
        });
    }
    //初始分数的选择器
    private void initializeSpinners() {
        SpinnerValueFactory.IntegerSpinnerValueFactory scoreValueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 10, 1);
        score.setValueFactory(scoreValueFactory);
        score.setEditable(true);
    }
    //初始化数据绑定
    private void initDataBind(){
        name.textProperty().bindBidirectional(activity.nameProperty());
        describe.textProperty().bindBidirectional(activity.descriptionProperty());
    }
}
